var regBox = { 
        uploadExcelFileType : /^.+\.(xlsx)|(xls)(doc)|(docx)(ppt)|(pptx)(pdf)|(jpg)(jpeg)|(png)(gif)|(bmp)(zip)|(rar)|(txt)$/,
        coverPicUrlFileType : /^.+\.(png)|(jpg)|(jpeg)|(gif)|(bmp)$/
    };  

$(document).ready(function(){
	document.domain = cookieDomain;
	 $.picRoomUtil();
	 
	 initPicUpload();
	 
	 initSectionSlt();
	 
	 initItems();

	 $('#addArticleForm').on({
		 click:function(){
			 saveArticle();
		 }
	 },'#saveButton');
	 
	 $('#addArticleForm').on({
		 click:function(){
			 saveAndPublishButton();
		 }
	 },'#saveAndPublishButton');

	 $('#addArticleForm').on({
		 click:function(){
			 previewArticle();
		 }
	 },'#previewButton');
	 
	 $('#addArticleForm').on({
		 change:function(){
			 var locChannelId = $("#locChannelId").val();
			 var themeId = $("#themeId").val();
			 if (locChannelId == "101") { 
				 $("#subTitleTr").show();
				 $("#keyWordTr").show();
				 $("#previewButton").show();
				 $("#editorTr").show();
				 $("#zcdwUrl").hide();
				 $("#xjhyTr").hide();
				 $("#videoUrlTr").hide();
				 $("#infoNoteTr").hide();
				 $("#attachTr").hide();
				 $('#titletd').html('<span style="color:red;">*</span>港口名称：');
				 $('#subtitleid').html('<span style="color:red;">*</span>企业名称：');
				 $('#keywordid').html('<span style="color:red;">*</span>客服QQ：');
			 } else{
				 $("#titleTr").show();
				 $("#subTitleTr").show();
				 $("#keyWordTr").show();
				 $("#infoNoteTr").show();
				 $("#attachTr").show();
				 $("#previewButton").show();
				 $("#editorTr").show();
				 $("#zcdwUrl").hide();
				 $("#xjhyTr").hide();
				 $("#videoUrlTr").hide();
				 $('#titletd').html('<span style="color:red;">*</span>标题：');
				 $('#subtitletd').html('副标题');
				 $('#keywordid').html('关键字');
			 }
			 
			 if(locChannelId != -1) {
				 var url = mainDomain + "/hy/article/findThemeMapByChannelId.action";
				 $.post(url,{'locChannelId':locChannelId},function(data){
						if(data.success == true){
							var locThemeSlt = '<option value="-1" selected>---请选择---</option>';
							$.each(data.data.themeMap,function(i,v){
								 locThemeSlt += '<option value="'+ i +'">' + v + '</option>';
							});
							$("#locThemeId").html(locThemeSlt);
						}
					});
			 }
		 }
	 },'#locChannelId');
	 
	 function checkContent() {
		 var result = true;
		 var ueditor =UE.getEditor('editor');
		 var txt = $.trim(ueditor.getContentTxt());
		 var content = ueditor.getContent();
		 if (txt != '') {
			 $.ajax({
				 type : "POST",
				 url : mainDomain + '/hy/article/checkSensitiveWord.action',
				 dataType:"JSON",
				 async: false,
				 data:{
					 txt : txt 
				 },
				 success : function(data){
					 $("#testNode").html(content);
					 if (data.success) {
						 if (!$.isEmptyObject(data.data)) {
							 result = false;
							 $.each(data.data,function(index,value){
								 var rg = new RegExp(value,'g');
								 content = content.replace(rg,'<span name="sensi" style="background-color: rgb(255, 0, 0);" class="sensi">'+ value +'</span>');
							 });
							 $("#testNode").html(content);
							 ueditor.setContent(content);
							 $.messager.alert('提示', '正方中包含敏感词，请检查。', 'error');
						 }
					 }else{
						 result = true;
						 var objs = document.getElementsByName("sensi");
						 $.each(objs , function(index,obj){
							 console.log( UE.dom.domUtils.hasClass( obj, "sensi" ));
							 UE.dom.domUtils.removeStyle( obj, 'background-color' );
						 });
						 ueditor.setContent( $("#testNode").html());
						 
					 }
				 }
			 });
		 } else {
			 result = false;
			 $.messager.alert('提示', '请输入资讯正文', 'error');
		 }
		 return result;
	 }
	 
	 function checkMustInput() {
		 var result = true;
		 var title = $.trim($('#title').val());
		 $('#title').val(title);
		 
		 var subTitle = $.trim($('#subTitle').val());
		 $('#subTitle').val(subTitle);
		 
		 var keyWord = $.trim($('#keyWord').val());
		 $('#keyWord').val(keyWord);
		 
		 var infoNote = $.trim($('#infoNote').val());
		 $('#infoNote').val(infoNote);
		 
		 var locChannelId = $("#locChannelId").val();
		 var locThemeId = $("#locThemeId").val();
		 if(locChannelId == -1 || locThemeId == -1) {
			 result = false;
			 $.messager.alert('提示', '请先选择类型', 'error');
			 return result;
		 }
		 
		 var chanel = $('#locChannelId').val();
		 if(chanel!=12){
			 if (title === '') {
				 result = false;
				 $.messager.alert('提示', '请输入资讯标题', 'error');
				 return result;
			 }
		 }else{
			 if (title === '') {
				 result = false;
				 $.messager.alert('提示', '请输入港口名称', 'error');
				 return result;
			 }
			 
			 if (subTitle === '') {
				 result = false;
				 $.messager.alert('提示', '请输入企业名称', 'error');
				 return result;
			 }
			 
			 if (keyWord === '') {
				 result = false;
				 $.messager.alert('提示', '请输入客服QQ', 'error');
				 return result;
			 }
		 }
		 

//		 if (infoNote === '') {
//			 result = false;
//			 $.messager.alert('提示', '请输入资讯摘要', 'error');
//			 return result;
//		 }
		
		 /* 
		 var uploadCoverPic = $('#uploadCoverPicBtn').val();
		 if (uploadCoverPic != '') {
			 if (!regBox.coverPicUrlFileType.test(uploadCoverPic.toLowerCase())) {
				 result = false;
				 $.messager.alert('提示', '请选择正确的封面图', 'error');
				 return result;
			 }
		 }
		 */
		 
//		 var uploadFile = $('#uploadFile').val();
//		 if (uploadCoverPic != '') {
//			 if (!regBox.uploadExcelFileType.test(uploadFile.toLowerCase())) {
//				 result = false;
//				 $.messager.alert('提示', '附件请使用常用的文件格式', 'error');
//				 return result;
//			 }
//		 }
		 
		 return result;
	 }
	 window.myValid = checkMustInput;
	 function checkForm() {
		 var result = true;
		 result = checkMustInput();
		 return result;
	 }
	 
	 function previewArticle() {
		 $('#addArticleForm').attr('action',mainDomain + '/hy/article/previewArticle.action');
		 $('#addArticleForm').attr('target','_blank');
		 $('#addArticleForm').submit();
	 }
	 
	 function saveAndPublishButton() {
		 $('#addArticleForm').attr('action',mainDomain + '/hy/article/addAndPublishArticle.action');
		 $('#addArticleForm').attr('target','');
		 $('#addArticleForm').submit();
	 }
	 
	 function saveArticle() {
		 $('#addArticleForm').attr('action',mainDomain + '/hy/article/addArticle.action');
		 $('#addArticleForm').attr('target','');
		 if (checkForm()) {
			 $('#addArticleForm').submit();
		 }
	 }
	 
	 $('body').on({
		 click:function(){
			 $('#oldFileId').val('');
			$(this).prev().prev().remove();
			$(this).prev().remove();
			$(this).remove();
		 }
	 },'#delFile');
	 
});

function initPicUpload(){
	var ueditor =UE.getEditor('editor');
	var content = $('#content_hidden_id').html();
	ueditor.addListener("ready", function () {
        ueditor.setContent(content);
        //干掉富文本上传图片按钮带事件
	   	 $('#editor').delegate('.edui-for-insertimage','mousedown',function(){
	   		 $(this).find('.edui-button-body').removeAttr('onclick');
	   		 return false;
	   	 });
	   	 //加入自己的图片插入事件
	   	 $('#editor').delegate('.edui-for-insertimage','click',function(){
	   		 var opts = $.picRoomUtil('options');
	   		 opts.callback=function(url){
	   			 for(var i=0;i<url.length;i++){
	   				 var value = '<div><center><img  src="'+url[i]+'"/></center></div>';
	   		         UE.getEditor('editor').execCommand('insertHtml', value);
	   			 }
	   		 };
	   		 var scollTop=document.documentElement.scrollTop;
	   		 $.picRoomUtil('move',{top:scollTop});
	   		 $.picRoomUtil('open');
	   		 return false;
	   	 });
	});
	
}

function initSectionSlt() {
	var infoId = $("#infoId").val();
	if(infoId != null && infoId != '') {
		var channelId = $("#channelId").val();
		var themeId = $("#themeId").val();
		$("#locChannelId").val(channelId);
			 var url = mainDomain + "/hy/article/findThemeMapByChannelId.action";
			 $.post(url,{'locChannelId':channelId},function(data){
					if(data.success == true){
						var locThemeSlt = '<option value="-1" selected>---请选择---</option>';
						$.each(data.data.themeMap,function(i,v){
							 locThemeSlt += '<option value="'+ i +'"';
							 if(themeId == i) {
								 locThemeSlt += ' selected';
							 }
							 locThemeSlt += '>' + v + '</option>';
						});
						$("#locThemeId").html(locThemeSlt);
					}
			});
	}
}

function initItems(){
	$("#titleTr").show();
	 $("#subTitleTr").show();
	 $("#keyWordTr").show();
	 $("#infoNoteTr").show();
	 $("#attachTr").show();
	 $("#previewButton").show();
	 $("#editorTr").show();
	 $("#zcdwUrl").hide();
	 $("#xjhyTr").hide();
	 $("#videoUrlTr").hide();
	 $('#titletd').html('<span style="color:red;">*</span>标题：');
	 $('#subtitletd').html('副标题');
	 $('#keywordid').html('关键字');
}


/**
 * 上传附件
 * Created by renbangjie on 15/11/14.
 */
//Dropzone.options.addArticleForm = {
//    autoProcessQueue: false,
//    paramName: 'articleFile',
//    uploadMultiple: false,
//    parallelUploads: 2,
//    maxFiles: 10,
//    url: mainDomain + '/article/addArticle.action',
//    
//    accept: function(file, done) {
//        if (file.name == "justinbieber.jpg") {
//          done("Naha, you don't.");
//          return;
//        }
//        done(); 
//    },
//    init: function() {
//        var myDropzone = this;
//        this.element.querySelector("input#saveButton").addEventListener("click", function(e) {
//            e.preventDefault();
//            e.stopPropagation();
//            //校验并提交
//            if (myValid()) {
//            	myDropzone.processQueue();
//   		 	}
//        });
//
//        this.on("sendingmultiple", function() {
//        	
//        });
//        this.on("successmultiple", function(files, response) {
//        	console.log("files:" + files);
//        	console.log("res:" + response);
//        });
//        this.on("errormultiple", function(files, response) {
//        	console.log("files:" + files);
//        	console.log("res:" + res);
//        });
//    }
//};























